from flask import Flask, request, redirect, url_for, flash, render_template, session

app = Flask(__name__)

app.secret_key = 'lyk03'


@app.route('/')
def index():
    return render_template('login.html')


@app.route('/home')
def home():
    username = session.get('username')
    if username is None:
        return redirect(url_for('login'))
    return render_template('home.html', username=username)


@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        if request.form['username'] != 'admin' or request.form['password'] != '123':
            flash('用户名或密码错误', category='error')
        else:
            session['username'] = request.form['username']
            session['password'] = request.form['password']
            flash('恭喜您, 登录成功！', category='info')
            return redirect(url_for('home'))
    return render_template('login.html')


if __name__ == '__main__':
    app.run()
